//
//  CWHomeRetweetView.swift
//  CWWeibo06
//
//  Created by ycw on 2017/7/26.
//  Copyright © 2017年 xiaowei. All rights reserved.
//

import UIKit
import SnapKit

/**
 转发微博的视图
 */

class CWHomeRetweetView: UIView {
    
    var retweetbottomConstraint: Constraint?
    
    var retweetViewModel: CWStatusViewModel? {
        
        didSet{
            guard let currentModel = retweetViewModel else {
                return
            }
            contentLabel.text = currentModel.contentText
            pictureView.picURLs = currentModel.statusModel?.retweeted_status?.pic_urls
            retweetbottomConstraint?.deactivate()
            //判断转发微博是是否有配图
            if let currentPicture = currentModel.statusModel?.retweeted_status?.pic_urls,currentPicture.count > 0 {
                //有配图视图
                pictureView.isHidden = false
                self.snp.makeConstraints({ (make) in
                    retweetbottomConstraint = make.bottom.equalTo(pictureView).offset(homeCellMargin).constraint
                })
                
            }else {
                //没有配图视图
                pictureView.isHidden = true
                self.snp.makeConstraints({ (make) in
                    retweetbottomConstraint = make.bottom.equalTo(contentLabel).offset(homeCellMargin).constraint
                })
            }
        }
    }
    
    fileprivate lazy var contentLabel: UILabel = {
       let label = UILabel(fontSize: 14, textColor: UIColor.lightGray)
        label.numberOfLines = 0
        return label
    }()
    
    //配图视图
    fileprivate lazy var pictureView: CWHomePictureView = {
       
        let picture = CWHomePictureView()
        picture.backgroundColor = self.backgroundColor
        return picture
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setupUI() {
        backgroundColor = UIColor(white: 0.95, alpha: 1)
        addSubview(contentLabel)
        addSubview(pictureView)
        contentLabel.snp.makeConstraints { (make) in
            make.top.leading.equalTo(self).offset(homeCellMargin)
            make.trailing.equalTo(self).offset(-homeCellMargin)
        }
        pictureView.snp.makeConstraints { (make) in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(homeCellMargin)
            make.size.equalTo(CGSize(width: 100, height: 100))
            retweetbottomConstraint = make.bottom.equalTo(self).offset(-homeCellMargin).constraint
        }
    }

}
